<?php
App::uses('AppController', 'Controller');
/**
 * Galleries Controller
 *
 * @property Gallery $Gallery
 */
class GalleriesController extends AppController {
	public function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allow('index');
	}
	
	public function beforeRender(){
		parent::beforeRender();
		switch($this->action){
			case 'display':
				$this->layout = 'default';
				break;
			case 'index':
				$this->layout = 'default';
				break;
			default:
				$this->layout = 'panel';
		}
		
	}
/**
 * index method
 *
 * @return void
 */
	public function index() {		
		$this->Gallery->recursive = -1;

		// najnowsze
		$options['joins'] = array(
			array('table' => 'galleries_uploads',
				        'alias' => 'GalleriesUpload',
				        'type' => 'left',
				        'conditions' => array('Gallery.id = GalleriesUpload.gallery_id'),
		
			),
			array('table' => 'uploads',
				        'alias' => 'Upload',
				        'type' => 'left',
				        'conditions' => array('GalleriesUpload.upload_id = Upload.id')
			)
		);
		$options['fields'] = array('Gallery.id','Gallery.name','Gallery.date','Gallery.slug','Upload.description','Upload.name');
		
		$options['conditions'] = array('Gallery.domain_id' => $this->Session->read('Config.Domain.Domain.id'));
		$options['limit'] = 2;
		$options['order'] = array('Gallery.date DESC');
		$options['group'] = array('Gallery.id');
		$this->set('galleries', $this->Gallery->find('all', $options));

		// gallery
		$this->Gallery->recursive = 1;
		if(isset($this->params['slug']) && !empty($this->params['slug'])){
			$default_gallery = $this->Gallery->findBySlug($this->params['slug']);
			if (!is_array($default_gallery) || empty($default_gallery)) {
				throw new NotFoundException(__d('gallery','Invalid gallery'));
			}
		}else if(isset($this->params['kategoria']) && !empty($this->params['kategoria'])){
			// wg kategorii
			$this->loadModel('GalleryCategory');
			$this->GalleryCategory->recursive = -1;
			$type = $this->GalleryCategory->findBySlug($this->params['kategoria']);
			$this->set('gallery_type',$type);
			$this->Gallery->recursive = -1;

			$options['joins'] = array(
			array('table' => 'galleries_uploads',
										        'alias' => 'GalleriesUpload',
										        'type' => 'left',
										        'conditions' => array('Gallery.id = GalleriesUpload.gallery_id'),
				
			),
			array('table' => 'uploads',
										        'alias' => 'Upload',
										        'type' => 'left',
										        'conditions' => array('GalleriesUpload.upload_id = Upload.id')
			),
			array('table' => 'galleries_gallery_categories',
										        'alias' => 'GalleriesGalleryCategory',
										        'type' => 'left',
										        'conditions' => array('Gallery.id = GalleriesGalleryCategory.gallery_id'),
			
			)
			);
			$options['fields'] = array('Gallery.id','Gallery.name','Gallery.date','Gallery.slug','Upload.description','Upload.name');
			$options['limit'] = null;
			$options['conditions'] = array('GalleriesGalleryCategory.gallery_category_id'=>$type['GalleryCategory']['id']);
			$galleries_by_type = $this->Gallery->find('all', $options);
						
			$this->set('galleries_by_type',$galleries_by_type);
			
		}
		
// 		else{
// 			default gallery
// 			$options['conditions'] = array('Gallery.domain_id' => $this->Session->read('Config.Domain.Domain.id'));
// 			$options['order'] = array('Gallery.date');
// 			$options['fields'] = null;
// 			$default_gallery = $this->Gallery->find('first', $options);
// 		}
		if(isset($default_gallery)) $this->set('default_gallery',$default_gallery);

		
		$this->Gallery->recursive = -1;
		// lista kategorii
		$options = array();
		$options['joins'] = array(
			array('table' => 'galleries_uploads',
							        'alias' => 'GalleriesUpload',
							        'type' => 'left',
							        'conditions' => array('Gallery.id = GalleriesUpload.gallery_id'),
			
			),
			array('table' => 'uploads',
							        'alias' => 'Upload',
							        'type' => 'left',
							        'conditions' => array('GalleriesUpload.upload_id = Upload.id')
			),
			array('table' => 'galleries_gallery_categories',
							        'alias' => 'GalleriesGalleryCategory',
							        'type' => 'left',
							        'conditions' => array('Gallery.id = GalleriesGalleryCategory.gallery_id'),
				
			),
			array('table' => 'gallery_categories',
							        'alias' => 'GalleryCategory',
							        'type' => 'left',
							        'conditions' => array('GalleriesGalleryCategory.gallery_category_id = GalleryCategory.id')
			),
		);
		$options['fields'] = array('Gallery.id','Gallery.name','Gallery.date','Gallery.slug','Upload.description','Upload.name','GalleryCategory.name','GalleryCategory.slug');
		$options['limit'] = null;
		$options['group'] = array('GalleryCategory.id');
		$kategorie = $this->Gallery->find('all', $options);
		$this->set('kategorie',$kategorie);
		
		
		
	}
	
	
	
	public function admin() {
		$this->Gallery->recursive = 0;
		$options['conditions'] = array('Gallery.domain_id' => $this->Session->read('Config.Domain.Domain.id'));
		$this->paginate = $options;
		$this->set('galleries', $this->paginate());
	}
	
	public function add_file($gallery_id = null) {
			
		if($this->FileUpload->success){
			$this->redirect(array('controller'=>'galleries','action' =>'add_file',$gallery_id));
		}else{
			$this->Session->setFlash($this->FileUpload->showErrors());
		}
		
		$data['Gallery']['id'] = $gallery_id;
		$data['Upload']['domain_id'] = $this->Session->read('Config.Domain.Domain.id');
		$this->request->data = $data;	
	}
	
	public function delete_file($id = null){
		$this->autoRender = false;
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
	
		$this->Gallery->Upload->id = $id;
		if (!$this->Gallery->Upload->exists()) {
			throw new NotFoundException(__d('articles','Invalid file'));
		}
	
		$upload = $this->Gallery->Upload->findById($id);
	
		$this->FileUpload->removeFile($upload['Upload']['name']);
		if($this->Gallery->Upload->delete($id)){
			$this->Session->setFlash('Upload deleted');			
			$this->redirect($this->referer());
		}
		$this->redirect($this->referer());
	}
	
/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Gallery->exists($id)) {
			throw new NotFoundException(__('Invalid gallery'));
		}
		$options = array('conditions' => array('Gallery.' . $this->Gallery->primaryKey => $id));
		$this->set('gallery', $this->Gallery->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->request->data['Gallery']['slug'] = strtolower( Inflector::slug($this->request->data['Gallery']['name'],'-') );
			$this->request->data['Gallery']['domain_id'] = $this->Session->read('Config.Domain.Domain.id');
				
			$this->Gallery->create();
			if ($this->Gallery->save($this->request->data)) {
				$this->Session->setFlash(__('The gallery has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The gallery could not be saved. Please, try again.'));
			}
		}
		$galleryCategories = $this->Gallery->GalleryCategory->find('list');
		$this->set(compact('galleryCategories'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Gallery->exists($id)) {
			throw new NotFoundException(__('Invalid gallery'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			$this->request->data['Gallery']['slug'] = strtolower( Inflector::slug($this->request->data['Gallery']['name'],'-') );
			$this->request->data['Gallery']['domain_id'] = $this->Session->read('Config.Domain.Domain.id');
			if ($this->Gallery->save($this->request->data)) {
				$this->Session->setFlash(__('The gallery has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The gallery could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Gallery.' . $this->Gallery->primaryKey => $id));
			$this->request->data = $this->Gallery->find('first', $options);
		}
		$galleryCategories = $this->Gallery->GalleryCategory->find('list');
		$this->set(compact('galleryCategories'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @throws MethodNotAllowedException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Gallery->id = $id;
		if (!$this->Gallery->exists()) {
			throw new NotFoundException(__('Invalid gallery'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Gallery->delete()) {
			$this->Session->setFlash(__('Gallery deleted'));
			$this->redirect(array('action' => 'admin'));
		}
		$this->Session->setFlash(__('Gallery was not deleted'));
		$this->redirect(array('action' => 'admin'));
	}
}
